package com.rzhd.coursepatriarch.common.utils;

import android.app.Activity;
import android.graphics.Typeface;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Field;

/**
 * @author yql
 * @date: 2018/6/26.
 */
public class TabLayoutUtils {

    public static void changeIndicator(final Activity mContext, final TabLayout tab, int marginLeft, int marginRight) {

        tab.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                tab.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                Class<?> tabLayout = tab.getClass();
                Field tabStrip = null;
                try {
                    tabStrip = tabLayout.getDeclaredField("mTabStrip");
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
                tabStrip.setAccessible(true);
                LinearLayout ll_tab = null;
                try {
                    ll_tab = (LinearLayout) tabStrip.get(tab);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                int maxLen = 0;
                int maxTextSize = 0;
                int tabCount = ll_tab.getChildCount();
                for (int i = 0; i < tabCount; i++) {
                    View child = ll_tab.getChildAt(i);
                    child.setPadding(0, 0, 0, 0);
                    if (child instanceof ViewGroup) {
                        ViewGroup viewGroup = (ViewGroup) child;
                        for (int j = 0; j < ll_tab.getChildCount(); j++) {
                            if (viewGroup.getChildAt(j) instanceof TextView) {
                                TextView tabTextView = (TextView) viewGroup.getChildAt(j);
                                int length = tabTextView.getText().length();
                                maxTextSize = (int) tabTextView.getTextSize() > maxTextSize ? (int) tabTextView.getTextSize() : maxTextSize;
                                maxLen = length > maxLen ? length : maxLen;
                            }
                        }

                    }

                    int margin = (tab.getWidth() / tabCount - (maxTextSize + ScreenUtils.dip2px(mContext, 2)) * maxLen) / 2 - ScreenUtils.dip2px(mContext, 2);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                    params.leftMargin = margin;
                    params.rightMargin = margin;
                    child.setLayoutParams(params);
                    child.invalidate();
                }
            }
        });
    }

    /**
     * 设置TabLayout中的字体是否要加粗
     *
     * @param tabLayout
     * @param isBold
     * @param position
     */
    public static void setTabTextStyle(TabLayout tabLayout, boolean isBold, int position) {
        try {
            LinearLayout ly = (LinearLayout) tabLayout.getChildAt(0);
            LinearLayout tabView = (LinearLayout) ly.getChildAt(position);
            if (null != tabView && tabView.getChildCount() > 0) {
                View view = tabView.getChildAt(1);
                if (null != view && view instanceof AppCompatTextView) {
                    if (isBold) {
                        ((AppCompatTextView) view).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        ((AppCompatTextView) view).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置TabLayout是否可以点击
     *
     * @param clickable
     */
    public static void setTabClick(TabLayout tabLayout, boolean clickable) {
        LinearLayout tabStrip = (LinearLayout) tabLayout.getChildAt(0);
        for (int i = 0; i < tabStrip.getChildCount(); i++) {
            View tabView = tabStrip.getChildAt(i);
            if (tabView != null) {
                tabView.setClickable(clickable);
            }
        }

        if (clickable) {
            tabLayout.setAlpha(1f);
        } else {
            tabLayout.setAlpha(0.5f);
        }
    }

}
